package com.example.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.config.ApplicationContextProvider;
import com.example.dto.ResultDTO;
import com.example.netty.nettyUserInfo;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;

import java.util.HashMap;
import java.util.Properties;

/**
 * Author by admin, Email xx@xx.com, Date on 2020/5/26.
 * PS: Not easy to write code, please indicate.
 */

@Slf4j
public class NettySendSysNotice {

    // 送Spring容器中获取消息监听器容器,处理订阅消息sysNotice
    SimpleMessageListenerContainer sysNoticeContainer = (SimpleMessageListenerContainer) ApplicationContextProvider.getApplicationContext()
            .getBean("sysNoticeContainer");
    //获取点赞监听容器
    SimpleMessageListenerContainer userNoticeContainer = (SimpleMessageListenerContainer) ApplicationContextProvider.getApplicationContext()
            .getBean("userNoticeContainer");
    //从Spring容器中获取RabbitTemplate
    RabbitTemplate rabbitTemplate = ApplicationContextProvider.getApplicationContext()
            .getBean(RabbitTemplate.class);

    public void sysNotionMsg(nettyUserInfo sendObj){
        //获取用户id
        String userId = sendObj.getUserId();
        //获取发送通道
        Channel channel = sendObj.getChannel();

        //只用完成新消息的提醒即可，只需要获取消息的数量
        //获取RabbitMQ的消息内容，并发送给用户
        RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate);
        //拼接获取队列名称
        String subQueueName = "article_subscribe_" + userId;
        //获取Rabbit的Properties容器
        Properties queueProperties = rabbitAdmin.getQueueProperties(subQueueName);
        //获取消息数量
        int noticeCount = 0;
        //判断Properties是否不为空
        if (queueProperties != null) {
            // 如果不为空，获取消息的数量
            noticeCount = (int) queueProperties.get("QUEUE_MESSAGE_COUNT");
        }
        //---------------------------获取点赞消息队列-----------------------------------------------
        //拼接获取队列名称
        String tupQueueName = "article_thumbup_" + userId;
        //获取Rabbit的Properties容器
        Properties userQueueProperties = rabbitAdmin.getQueueProperties(tupQueueName);
        //获取消息数量
        int userNoticeCount = 0;
        //判断Properties是否不为空
        if (userQueueProperties != null) {
            // 如果不为空，获取消息的数量
            userNoticeCount = (int) userQueueProperties.get("QUEUE_MESSAGE_COUNT");
        }
        //封装返回的数据
        HashMap countMap = new HashMap();
        //发送订阅消息
        countMap.put("sysNoticeCount", noticeCount);
        //发送点赞消息
        countMap.put("userNoticeCount", userNoticeCount  );

        ResultDTO resultDTO = ResultDTO.okOf("---系统通知---",countMap);
        //把数据发送给用户
        channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(resultDTO)));

        //为用户的消息通知队列注册监听器，便于用户在线的时候，
        //一旦有消息，可以主动推送给用户，不需要用户请求服务器获取数据
        sysNoticeContainer.addQueueNames(tupQueueName);
        sysNoticeContainer.addQueueNames(subQueueName);
//        userNoticeContainer.addQueueNames(userQueueName);
        //把消息从队列里面清空，否则MQ消息监听器会再次消费一次
        if (noticeCount > 0) {
            rabbitAdmin.purgeQueue(tupQueueName, true);
        }
        //把消息从队列里面清空，否则MQ消息监听器会再次消费一次
        if (userNoticeCount > 0) {
            rabbitAdmin.purgeQueue(subQueueName, true);
        }
    }
}
